#include "Smart_Classroom.h"
#include "qmessagebox.h"
#include <Windows.h>
#include "../HCCamera/HCCamera.h"
#include <PoseDetcetion.h>
#define TIMER_TIMEOUT 1

DWORD WINAPI Start_Camera_Server(LPVOID pvoid) {
	pDeviceInfo device = (pDeviceInfo)pvoid;
	if (HCCamera_login(device->ipaddress, device->port, device->username, device->password)) {
		return 1;
	}
	return 0;
}
DWORD WINAPI Start_Pose_Server(LPVOID pvoid) {
	PoseDetcetion::init();
	Sleep(1000);
	PoseDetcetion::HCCSDK_POSE_SERVER();
	return 0;
}

Smart_Classroom::Smart_Classroom(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	config.init();
	CameraConfig = config.getCameraConfig();
	Set_Connect();
	ui.Button_Stop->setEnabled(false);

	for (int index =0; index < std::stoi((CameraConfig.value("number").toLatin1().data())); index++)
	{
		
		QListWidgetItem *item = new QListWidgetItem;
		QString number = QString::number(index, 10);
		QString value = CameraConfig.value(number);
		if (index == 0) {
			device.ipaddress = new char[strlen(value.toLatin1().data())]();
			strcpy(device.ipaddress,value.toLatin1().data());

			device.password = new char[strlen(CameraConfig.value("password").toLatin1().data())]();
			strcpy(device.password,CameraConfig.value("password").toLatin1().data());

			device.username = new char[strlen(CameraConfig.value("username").toLatin1().data())];
			strcpy(device.username, CameraConfig.value("username").toLatin1().data());

			device.port = new char[strlen(CameraConfig.value("port").toLatin1().data())]();
			strcpy(device.port, CameraConfig.value("port").toLatin1().data());
		}
		item->setText(value);
		ui.list_Camera->addItem(item);
	}

}
void Smart_Classroom::Set_Connect() {
	connect(ui.Button_Start, SIGNAL(clicked()), this, SLOT(Button_Start_Clicked()));
	connect(ui.Button_Stop, SIGNAL(clicked()), this, SLOT(Button_Stop_Clicked()));
	connect(ui.Button_Next, SIGNAL(clicked()), this, SLOT(Button_Next_Clicked()));
	connect(ui.Button_Previous, SIGNAL(clicked()), this, SLOT(Button_Previous_Clicked()));
	connect(ui.list_Camera, SIGNAL(itemSelectionChanged()), SLOT(SelectItem_Changed()));
}
void Smart_Classroom::Button_Start_Clicked() {
	if (is_item_select_changed&&thread_camera != NULL) {
		HCCamera_logout();
		TerminateThread(thread_camera, 0);
		Sleep(3000);
		thread_camera = CreateThread(NULL, 0, Start_Camera_Server, &device, 0, NULL);
	}
	if (!is_pose_start) {
		is_pose_start = true;
		thread_pose = CreateThread(NULL, 0, Start_Pose_Server, NULL, 0, NULL);
		Sleep(2000);
		thread_camera = CreateThread(NULL, 0, Start_Camera_Server, &device, 0, NULL);
		Sleep(200);

	}
	is_item_select_changed = false;
	ui.Button_Start->setEnabled(false);
	ui.Button_Stop->setEnabled(true);
	this->timer_ID = this->startTimer(TIMER_TIMEOUT);
	Send_Status_Message("Start");
}
void Smart_Classroom::Button_Stop_Clicked() {
	ui.Button_Start->setEnabled(true);
	ui.Button_Stop->setEnabled(false);
	killTimer(this->timer_ID);
	Send_Status_Message("Stoped");
}
void Smart_Classroom::Button_Next_Clicked() {
	Send_Status_Message("Next");
}
void Smart_Classroom::Button_Previous_Clicked() {

	Send_Status_Message("Previous");
}
void Smart_Classroom::Send_Status_Message(QString msg) {
	if (this->msgLabel)statusBar()->removeWidget(this->msgLabel);
	this->msgLabel = new QLabel;
	this->msgLabel->setMinimumSize(msgLabel->sizeHint());
	this->msgLabel->setAlignment(Qt::AlignHCenter);
	this->msgLabel->setText(msg);
	this->statusBar()->addWidget(msgLabel);
}
void Smart_Classroom::timerEvent(QTimerEvent *event) {
	if (event->timerId() == timer_ID) {
		Message_Queue_Receive();
	}
}
Smart_Classroom::~Smart_Classroom() {
	killTimer(this->timer_ID);
	exit(0);
}
void Smart_Classroom::Message_Queue_Receive() {
	if (PoseDetcetion::image_msg_queue_qt.size() >= 10) {
		if (scene != NULL)delete scene;
		QImage image((uchar *)(PoseDetcetion::image_msg_queue_qt.front().data), PoseDetcetion::image_msg_queue_qt.front().cols, PoseDetcetion::image_msg_queue_qt.front().rows, QImage::Format_RGB888);
		scene = new QGraphicsScene;
		scene->addPixmap(QPixmap::fromImage(image));
		ui.graphicsView->setScene(scene);
		ui.graphicsView->show();
		PoseDetcetion::image_msg_queue_qt.pop();
	}
}
void Smart_Classroom::SelectItem_Changed() {
	is_item_select_changed = true;
	QListWidgetItem *item = ui.list_Camera->currentItem();
	QString ipaddress = item->text();
	//QMessageBox::information(NULL, "Title", ipaddress, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
	delete device.ipaddress;
	device.ipaddress = new char[strlen(ipaddress.toLatin1().data())]();;
	strcpy(device.ipaddress, ipaddress.toLatin1().data());
}